// OptionReader.cs
//

namespace ServiceAdmin.Config
{
	using ServiceAdmin;

	using System;
	using System.Collections;
	using System.Collections.Specialized;
	using System.Drawing;
	using System.Globalization;
	using System.Reflection;
	using System.Resources;
	using System.Windows.Forms;


	/// <summary>
	/// Summary description for OptionReader.
	/// </summary>
	public class OptionReader
	{
		private const string TIMER					= "Timer";
		private const string MACHINE_NAME			= "Machine Name";
		private const string SERVICE_NAME			= "Service Name";
		private const string LANGUAGE				= "Language";

		private int					_timer			= 2500;
		private string				_machineName	= "";
		private string				_serviceName	= "";
		private string				_language		= OptionHandler.DEFAULT_VALUE;
		private bool				_readError		= false;

		private NameValueCollection	_resXEntries	= new NameValueCollection();
		private IResourceReader		_reader			= null;


		public IResourceReader Reader
		{
			get { return _reader; }
		}


		public int Timer
		{
			get { return _timer; }
		}


		public string MachineName
		{
			get { return _machineName; }
		}


		public string ServiceName
		{
			get { return _serviceName; }
		}


		public string Language
		{
			get { return _language; }
		}


		public bool IsValidReader
		{
			get { return (_machineName != null && _serviceName != null && _language != null && !_readError); }
		}


		/// <summary>
		/// Creates a new instance of OptionReader
		/// </summary>
		public OptionReader(string resXPath)
		{
			try
			{
				_reader = new ResXResourceReader(resXPath);
				ReadConfig(_language);
			}
			catch
			{
				_readError = true;
			}
			finally
			{
				if (_reader != null)
				{
					_reader.Close();
				}
			}
		}
		
		private void ReadConfig(string language)
		{
            foreach(DictionaryEntry myEntry in _reader)
            {
                _resXEntries.Add(myEntry.Key as string, myEntry.Value as string);
            }

            _timer          = Convert.ToInt32(_resXEntries[TIMER]);
            _machineName    = _resXEntries[MACHINE_NAME];
            _serviceName    = _resXEntries[SERVICE_NAME];
            _language       = _resXEntries[LANGUAGE];
		}
	}
}